import { Controller } from 'egg';

interface tokenType {
  account: string;
  password: string;
  type: number;
}
interface resultType {
  code: number;
  message: string;
  data: any[]
}
// interface searchInfoType{
//   account: string;
//   username: string;
//   sex: string;
//   type: null | number;
//   pageSize: number;
//   current: number;
// }
export default class UserController extends Controller {
  private validateToken():tokenType {
    const token:any = this.ctx.request.header.authorization;
    const decode:string = JSON.stringify(this.ctx.app.jwt.verify(token, this.app.config.jwt.secret));
    const decodeParse:tokenType = JSON.parse(decode);
    return decodeParse;
  }
  /**
   * 查询所有用户
   */
  public async queryUserAll() {
    const { ctx } = this;
    const decode:tokenType = this.validateToken();
    const userInfo = ctx.query;
    const result = await ctx.service.userService.queryUserAll(userInfo, decode) as resultType;
    ctx.body = result;
  }

  /**
   * 删除用户
   */
  public async delUser() {
    const { ctx } = this;
    ctx.validate({ id: 'int' }, ctx.request.body);
    const decode:tokenType = this.validateToken();
    const result:resultType = await ctx.service.userService.delUser(ctx.request.body.id, decode) as resultType;
    ctx.body = result;
  }

  /**
   * 修改用户
   */
  public async updateUser() {
    const { ctx } = this;
    ctx.validate({ id: 'int', account: 'string', username: 'string', sex: 'string', age: 'number', type: 'int' }, ctx.request.body);
    const decode:tokenType = this.validateToken();
    const result:resultType = await ctx.service.userService.updateUser(ctx.request.body.id, ctx.request.body, decode) as resultType;
    ctx.body = result;
  }
}
